﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BankCreditPortfolio.DAL.Entities;
using BankCreditPortfolio.DAL.Services;
using BankCreditPortfolio.Models;

namespace BankCreditPortfolio.Areas.Admin.Models
{
    public class AddEmployeeModel : IBaseModel<Banker>
    {
        [Required]
        [DataType(DataType.EmailAddress)]
        [RegularExpression(@"^[-a-z0-9!#$%&'*+/=?^_`{|}~]+(?:\.[-a-z0-9!#$%&'*+/=?^_`{|}~]+)*@(?:[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])?\.)*(?:aero|arpa|asia|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|[a-z][a-z])$", ErrorMessage = "This e-mail is not correct.")]
        [Display(Name = "Email address")]
        public string Email { get; set; }

        [Required]
        [Display(Name = "Role")]
        public int Role { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 8)]
        [DataType(DataType.Password)]
        [RegularExpression(@"^.*(?=.*\d)(?=.*[A-Za-z]).*$", ErrorMessage = "The {0} must contain at least 1 latin symbol and 1 number.")]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }

        [Required, StringLength(40)]
        [Display(Name = "First name")]
        [RegularExpression(@"^ *([A-Z][a-z]*[\s-']?)*([A-Z][a-z]*)+ *$", ErrorMessage = "The {0} is incorrect.")]
        public string FirstName { get; set; }

        [Required, StringLength(40)]
        [Display(Name = "Last name")]
        [RegularExpression(@"^ *([A-Z][a-z]*[\s-']?)*([A-Z][a-z]*)+ *$", ErrorMessage = "The {0} is incorrect.")]
        public string LastName { get; set; }

        [Required, StringLength(40)]
        [Display(Name = "Middle name")]
        [RegularExpression(@"^ *([A-Z][a-z]*[\s-']?)*([A-Z][a-z]*)+ *$", ErrorMessage = "The {0} is incorrect.")]
        public string MiddleName { get; set; }

        [Required]
        [StringLength(14, ErrorMessage = "The {0} must be {2} characters long.", MinimumLength = 14)]
        [RegularExpression(@"^ *[0-9]{7}[a-zA-Z][0-9]{3}[a-zA-Z]{2}[0-9] *$", ErrorMessage = "The {0} is in incorrect format.")]
        [Display(Name = "Passport Personal Number")]
        public string PersonalId { get; set; }

        [Required]
        [DataType(DataType.Date)]
        [DisplayFormat(NullDisplayText = "", DataFormatString = "{0:MM/dd/yyyy}")]
        [RegularExpression(@"^([1-9]|0[1-9]|1[012])/([1-9]|0[1-9]|[12][0-9]|3[01])/[0-9]{4}$", ErrorMessage = "The {0} is incorrectly formatted. Right format: MM/DD/YYYY.")]
        [Display(Name = "Date of passport delivery")]
        public string PassportDate { get; set; }

        [Required, StringLength(255, MinimumLength = 4)]
        [Display(Name = "Passport issuing authority")]
        public string PassportIssuingAuthority { get; set; }

        [Required, StringLength(255, MinimumLength = 4)]
        [Display(Name = "Address")]
        public string Address { get; set; }

        [Required]
        [DataType(DataType.PhoneNumber)]
        [Display(Name = "Phone")]
        [RegularExpression(@"^\+[0-9]{5} [0-9]{7}$", ErrorMessage = "The {0} in not valid for Belarus.")]
        public string Phone { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            var accountService = new AccountService();
            if (accountService.GetByEmail(Email) != null)
            {
                yield return
                    new ValidationResult("The e-mail address already exists. Please enter a different e-mail address.",
                                         new[] { "Email" });
            }

            DateTime date;
            if (!DateTime.TryParse(PassportDate, CultureInfo.CreateSpecificCulture("en-US"), DateTimeStyles.None, out date))
            {
                yield return new ValidationResult("The passport delivery date is incorrect. Format: MM/DD/YYYY.",
                    new[] { "PassportDate" });
            }
            else
            {
                var minDate = DateTime.Today.AddYears(-10);
                var maxDate = DateTime.Today;

                if (date < minDate || date > maxDate)
                {
                    yield return new ValidationResult(String.Format("The passport delivery date must be between {0} and {1}.",
                        minDate.ToShortDateString(), maxDate.ToShortDateString()),
                        new[] { "PassportDate" });
                }
            }
        }

        public IBaseModel<Banker> MapFrom(Banker @from)
        {
            throw new NotImplementedException();
        }

        public Banker MapTo(Banker to)
        {
            throw new NotImplementedException();
        }
    }
}